package com.yunwaikeji.gy_tool_library.utils.file.delete;

import android.os.AsyncTask;
import android.util.Log;

import com.yunwaikeji.gy_tool_library.info.GYFileInfo;
import com.yunwaikeji.gy_tool_library.utils.file.GYFileUtils;

import java.io.File;
import java.util.List;

public class GYFileDeleteTask extends AsyncTask<Object, Object, Object> {
    private FileDeleteListener fileDeleteListener;
    private File file;
    private GYFileInfo.DeleteType deleteType = GYFileInfo.DeleteType.DEFAULT;
    private boolean isDeleting = false;

    public GYFileDeleteTask() {
    }

    public GYFileDeleteTask(File file) {
        this.file = file;
    }

    public GYFileDeleteTask(String file) {
        this.file = new File(file);
    }

    public GYFileDeleteTask setFile(File file) {
        this.file = file;
        return this;
    }

    public GYFileDeleteTask setFile(String file) {
        return setFile(new File(file));
    }

    public GYFileDeleteTask setDeleteType(GYFileInfo.DeleteType deleteType) {
        this.deleteType = deleteType;
        return this;
    }

    public GYFileDeleteTask setFileDeleteListener(FileDeleteListener fileDeleteListener) {
        this.fileDeleteListener = fileDeleteListener;
        return this;
    }

    public void start() {
        execute();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (fileDeleteListener != null) {
            fileDeleteListener.start();
        }
    }

    @Override
    protected Object doInBackground(Object... objects) {
        if (!isDeleting) {
            isDeleting = true;
            if (!file.exists()) {
                showErrorLog("文件或文件夹不存在：" + file);
                return null;
            }
            if (deleteType == GYFileInfo.DeleteType.KEEP_NOW_FOLDER) {
                List<File> fileList = GYFileUtils.getFiles(file);
                if (fileList.size() > 0) {
                    fileList.remove(fileList.size() - 1);
                }
                for (File file : fileList) {
                    loading(file);
                    if (file.delete()) {
                        success(file);
                    } else {
                        showErrorLog(file);
                    }
                }
            } else if (deleteType == GYFileInfo.DeleteType.KEEP_ALL_FOLDER) {
                for (File file : GYFileUtils.getFiles(file, GYFileInfo.FileType.FILE)) {
                    loading(file);
                    if (file.delete()) {
                        success(file);
                    } else {
                        showErrorLog(file);
                    }
                }
            } else {
                for (File file : GYFileUtils.getFiles(file)) {
                    loading(file);
                    if (file.delete()) {
                        success(file);
                    } else {
                        showErrorLog(file);
                    }
                }
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        if (fileDeleteListener != null) {
            fileDeleteListener.finish();
        }
    }

    private void loading(File file) {
        if (fileDeleteListener != null) {
            fileDeleteListener.loading(file);
        }
    }

    private void success(File file) {
        if (fileDeleteListener != null) {
            fileDeleteListener.success(file);
        }
    }

    public void stop() {
        if (isDeleting) {
            cancel(false);
            isDeleting = false;
            if (fileDeleteListener != null) {
                fileDeleteListener.stop();
            }
        }
    }

    private void showErrorLog(String error) {
        Log.e(getClass().getSimpleName(), error);
        isDeleting = false;
        if (fileDeleteListener != null) {
            fileDeleteListener.error(error);
        }
    }

    private void showErrorLog(File file) {
        showErrorLog("删除失败：" + file.getPath());
    }
}
